package cj.web.portal.controllers;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.kly.domain.GroupDep;
import cj.web.kly.domain.GroupInfor;
import cj.web.kly.domain.GroupPeople;
import cj.web.kly.domain.News;
import cj.web.login.controllers.BaseController;
import cj.web.portal.service.ColumnsNewsService;
import cj.web.portal.service.PeopleService;

/**
 * 所情概况
 * @author Administrator
 *
 */
@Path("/resource")
public class ResourceController extends BaseController {

	private static Logger logger = Logger.getLogger(ResourceController.class.getName());
	
	private static String resourceMemuKey = "24";
	
	@Autowired
	private PeopleService peopleService;
	@Autowired
	private ColumnsNewsService columnsNewsService;
	
	//资源所简介 24
	@Get("aboutContent")
	public String aboutContent(Model model,@Param("columnsId") int columnsId) throws ParseException {
		List<News> newslis = columnsNewsService.queryForContentById(columnsId, 0, 1);
		model.add("news",newslis.get(0));
		model.add("resourceMemuKey",resourceMemuKey);
		return "/views/portal/resource/aboutContent.vm";
	}
	
	//资源所发展历程 25
	@Get("history")
	public String history(Model model,@Param("columnsId") int columnsId) throws ParseException {
		List<News> newslis = columnsNewsService.queryForContentById(columnsId, 0, 1);
		model.add("news",newslis.get(0));
		model.add("resourceMemuKey",resourceMemuKey);
		return "/views/portal/resource/history.vm";
	}
	
	//历届所长 36
	@Get("previousDirector")
	public String previousDirector(Model model,@Param("depId") int depId) throws ParseException {
		model.add("depId",depId);
		model.add("resourceMemuKey",resourceMemuKey);
		return "/views/portal/resource/previousDirector.vm";
	}
	
	/**
	 * 请求方法
	 * @param columnsId
	 * @param start
	 * @param length
	 * @return
	 * @throws ParseException
	 */
	@Post("getGroupPeopleList")
	public String queryList(@Param("depId") int depId,
			@Param("start") int start,@Param("length") int length) throws ParseException {
	
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		Map rep = peopleService.query(depId, start*length, length);
		List<News> objs = (List<News>) rep.get("groupPeoplelis");
		
		int size = (int) rep.get("size");
		
		jo.put("list", objs);
		jo.put("totalCount", size);
		return "@"+jo.toString();
	}
	
	//现任所领导 37
	@Get("currentDirector")
	public String currentDirector(Model model,@Param("groupId") int groupId) throws ParseException {
		List<Map> peoplelis = peopleService.queryCurrentDirectorlis(groupId);
		model.add("peoplelis",peoplelis);
		model.add("resourceMemuKey",resourceMemuKey);
		return "/views/portal/resource/currentDirector.vm";
	}
	
	//院士风采 42
	@Get("directorStyle")
	public String directorStyle(Model model,@Param("depId") int depId) throws ParseException {
		List<GroupPeople> peoplelis = peopleService.queryDetaillis(depId);
		model.add("peoplelis",peoplelis);
		model.add("resourceMemuKey",resourceMemuKey);
		return "/views/portal/resource/directorStyle.vm";
	}
	
	//机构概览 26
	@Get("organIntro")
	public String organIntro(Model model,@Param("columnsId") int columnsId) throws ParseException {
		List<News> newslis = columnsNewsService.queryForContentById(columnsId, 0, 1);
		model.add("news",newslis.get(0));
		model.add("resourceMemuKey",resourceMemuKey);
		return "/views/portal/resource/organIntro.vm";
	}
	
	//人物详情页面-部门其他人员
	@Get("organPersonDetail")
	public String detailPage(Model model,@Param("peopleId") int peopleId) throws Exception {
		Map rep = peopleService.queryByPeopleId(peopleId);
		
		List<GroupPeople> groupPeoplelis = (List<GroupPeople>) rep.get("groupPeoplelis");
		GroupInfor groupInfor = (GroupInfor) rep.get("groupInfor");
		GroupDep groupDep = (GroupDep) rep.get("groupDep");
		
		model.add("groupPeoplelis",groupPeoplelis);
		model.add("groupInfor",groupInfor);
		model.add("groupDep",groupDep);
		model.add("peopleId",peopleId);
		model.add("resourceMemuKey",resourceMemuKey);
		return "/views/portal/resource/directorDetail.vm";
	}
	
	//查询用户详情
	@Post("organPersonDetailFun")
	public String detail(@Param("peopleId") int peopleId) throws ParseException {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		GroupPeople people = peopleService.queryById(peopleId);
		jo.put("people",people);
		return "@"+jo.toString();
	}
}